package cn.wecloud.im.multiplayervideodemo.utils

import cn.wecloud.im.multiplayervideodemo.App
import cn.wecloud.im.multiplayervideodemo.R
import java.text.SimpleDateFormat
import java.util.*
import java.util.Formatter

object DateFormatHelper {

    fun formatDate(timeStamp: Long?, format: String = "yyyy-MM-dd HH:mm:ss"): String {
        val dateFormat = SimpleDateFormat(format, Locale.getDefault())
        return dateFormat.format(Date(timeStamp ?: 0))
    }

    fun getDuration(duration: Long): String {
        val second = duration.toInt()
        if (second > 60) {
            return formatSecond(second)
        }
        return "$duration\""
    }

    private val sf = SimpleDateFormat("yyyyMMdd_HHmmssSS")

    /**
     * 根据时间戳创建文件名
     *
     * @return
     */
    fun getCreateFileName(): String? {
        val millis = System.currentTimeMillis()
        return sf.format(millis)
    }

    /**
     * 根据时间戳创建文件名
     *
     * @param prefix 前缀名
     * @return
     */
    fun getCreateFileName(prefix: String): String? {
        val millis = System.currentTimeMillis()
        return prefix + sf.format(millis)
    }


    fun stringForTime(second: Int): String {
        val seconds = second % 60;
        val minutes = (second / 60) % 60;
        return Formatter().format("%02d:%02d", minutes, seconds).toString();
    }


    fun getOldDate(distanceDay: Int): Long {
        val beginDate = Date()
        val date = Calendar.getInstance()
        date.time = beginDate
        date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay)
        return date.timeInMillis
    }

    fun getMonthGroup(timestamps: Long): String {
        val calNow = Calendar.getInstance()
        val day = calNow.get(Calendar.DATE)
        val month = calNow.get(Calendar.MONTH) + 1
        val year = calNow.get(Calendar.YEAR)
        val cal = Calendar.getInstance()
        cal.timeInMillis = timestamps
        val tempMonth = cal.get(Calendar.MONTH) + 1
        return if (tempMonth == month) {
            //表示本月发送的
            "最近"
        } else if (month - tempMonth == 1) {
            //表示本月发送的
            "上个月"
        } else {
            tempMonth.toString() + "月"
        }
    }

    private fun getTime(d: Long): String {
        return if (d < 10) {
            "0${d}"
        } else {
            d.toString()
        }
    }


    fun formatSecond2(timeStamp: Long): String {
        val second = timeStamp / 1000
        val hours = second / (60 * 60)
        val minutes = second / 60 - hours * 60
        val seconds = second - minutes * 60 - hours * 60 * 60
        return when {
            hours > 0 -> {
                "${getTime(hours)}:${getTime(minutes)}:${getTime(seconds)}"
            }
            else -> {
                "${getTime(minutes)}:${getTime(seconds)}"
            }
        }


    }

    private fun formatSecond(second: Int): String {
        var formatTime = "0"
        val format: String
        val array: Array<Any>
        val hours = second / (60 * 60)
        val minutes = second / 60 - hours * 60
        val seconds = second - minutes * 60 - hours * 60 * 60
        when {
            hours > 0 -> {
                format = "%1$,d:%2$,d:%3$,d"
                array = arrayOf(hours, minutes, seconds)
            }
            minutes > 0 -> {
                format = "%1$,d'%2$,d\""
                array = arrayOf(minutes, seconds)
            }
            else -> {
                format = "%1$,d'"
                array = arrayOf(seconds)
            }
        }
        formatTime = String.format(format, *array)

        return formatTime

    }


    fun getDateAfter(day: Int): String {
        val context = App.instance
        val now = Calendar.getInstance()
        now.time = Date(System.currentTimeMillis())
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day)
        val pattern = "MM${context.getString(R.string.month)}dd${context.getString(R.string.day)}"
        val dateFormat = SimpleDateFormat(pattern, Locale.getDefault())
        return dateFormat.format(now.time)
    }
}